import akka.kernel.Bootable
import akka.actor._
import com.typesafe.config.ConfigFactory

class SupervisorActor extends Actor 
{
  val workers = 2
  var doneCount = 0
  var work : List[Any] = List()
  
  def receive = {
    case Work(start, end, length) => 
      {
		  val localWorker = context.system.actorSelection(Connections.localWorker)
		  localWorker ! Work(start, end/2, length)
		  
		  val remoteWorker = context.system.actorSelection(Connections.remoteWorker)
		  remoteWorker ! Work((end/2) + 1, end, length)
		  
		  println("Sent Work")
      }
    case Done(l) =>
      {
        doneCount = doneCount + 1
        work ++= l
        if (doneCount == workers)
        {
          work.foreach { println }
          println("Done. Shuting Down")
          context.system.shutdown()
        }
      }
    case _ =>
      {
        println("Unknown msg at supervisor")
      }
  }
}

object Supervisor extends App {
  println("Started Supervisor")
  val system = ActorSystem("SupervisorApplication", ConfigFactory.load.getConfig("supervisor"))
  val supervisor = system.actorOf(Props[SupervisorActor], "Supervisor")
  
  val startTime = System.currentTimeMillis();
  supervisor ! Work("1".toLong, args(0).toLong, args(1).toLong)
  system.awaitTermination()
  
  val endTime   = System.currentTimeMillis()
  val totalTime = (endTime - startTime)
  println("Time: " + totalTime + " ms")
}


